package com.yunfeisoft.business.controller;

import com.yunfeisoft.business.model.DailyLifeActivityAssessment;
import com.yunfeisoft.business.model.Elder;
import com.yunfeisoft.business.service.inter.DailyLifeActivityAssessmentService;
import com.yunfeisoft.business.service.inter.ElderService;
import com.yunfeisoft.common.base.BaseController;
import com.yunfeisoft.common.utils.Page;
import com.yunfeisoft.common.utils.Response;
import com.yunfeisoft.common.utils.ResponseUtils;
import com.yunfeisoft.common.utils.Validator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: DailyLifeActivityAssessmentController
 * Description: 日常生活活动评估表Controller
 * Author: Jackie liu
 * Date: 2020-05-22
 */
@Controller
public class DailyLifeActivityAssessmentController extends BaseController {

    @Autowired
    private DailyLifeActivityAssessmentService dailyLifeActivityAssessmentService;

    @Autowired
    private ElderService elderService;

    /**
     * 添加日常生活活动评估表
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/dailyLifeActivityAssessment/save", method = RequestMethod.POST)
    @ResponseBody
    public Response save(DailyLifeActivityAssessment record, HttpServletRequest request, HttpServletResponse response) {
        Validator validator = new Validator();
        validator.required(request, "elderId", "姓名为空");
        validator.required(request, "eatScore", "进食情况为空");
        validator.required(request, "washScore", "洗澡情况为空");
        validator.required(request, "retouchScore", "修饰情况为空");
        validator.required(request, "wareScore", "穿衣情况为空");
        validator.required(request, "stoolControlScore", "大便控制情况为空");
        validator.required(request, "peeControlScore", "小便控制情况为空");
        validator.required(request, "toiletScore", "入厕情况为空");
        validator.required(request, "bedChairTransferScore", "床椅转移情况为空");
        validator.required(request, "walkGroundScore", "平地行走情况为空");
        validator.required(request, "walkStairsScore", "上下楼梯情况为空");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }
        // 计算总得分
        Integer totalScore = record.getEatScore() + record.getBedChairTransferScore() + record.getPeeControlScore()
                + record.getRetouchScore() + record.getStoolControlScore() + record.getToiletScore() + record.getWalkGroundScore()
                + record.getWalkStairsScore() + record.getWashScore() + record.getWareScore();
        record.setTotalScore(totalScore);
        // 设置评分等级
        if (totalScore == 100) {
            record.setLevel(0);
        } else if (totalScore <= 95 && totalScore >= 65) {
            record.setLevel(1);
        } else if (totalScore <= 60 && totalScore >= 45) {
            record.setLevel(2);
        } else if (totalScore <= 40) {
            record.setLevel(3);
        }
        dailyLifeActivityAssessmentService.save(record);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 修改日常生活活动评估表
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/dailyLifeActivityAssessment/modify", method = RequestMethod.POST)
    @ResponseBody
    public Response modify(DailyLifeActivityAssessment record, HttpServletRequest request, HttpServletResponse response) {
        Validator validator = new Validator();
        validator.required(request, "elderId", "姓名为空");
        validator.required(request, "eatScore", "进食情况为空");
        validator.required(request, "washScore", "洗澡情况为空");
        validator.required(request, "retouchScore", "修饰情况为空");
        validator.required(request, "wareScore", "穿衣情况为空");
        validator.required(request, "stoolControlScore", "大便控制情况为空");
        validator.required(request, "peeControlScore", "小便控制情况为空");
        validator.required(request, "toiletScore", "入厕情况为空");
        validator.required(request, "bedChairTransferScore", "床椅转移情况为空");
        validator.required(request, "walkGroundScore", "平地行走情况为空");
        validator.required(request, "walkStairsScore", "上下楼梯情况为空");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }
        // 计算总得分
        Integer totalScore = record.getEatScore() + record.getBedChairTransferScore() + record.getPeeControlScore()
                + record.getRetouchScore() + record.getStoolControlScore() + record.getToiletScore() + record.getWalkGroundScore()
                + record.getWalkStairsScore() + record.getWashScore() + record.getWareScore();
        record.setTotalScore(totalScore);
        // 设置评分等级
        if (totalScore == 100) {
            record.setLevel(0);
        } else if (totalScore <= 95 && totalScore >= 65) {
            record.setLevel(1);
        } else if (totalScore <= 60 && totalScore >= 45) {
            record.setLevel(2);
        } else if (totalScore <= 40) {
            record.setLevel(3);
        }
        dailyLifeActivityAssessmentService.modify(record);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 查询日常生活活动评估表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/dailyLifeActivityAssessment/query", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response query(HttpServletRequest request, HttpServletResponse response) {
        String id = ServletRequestUtils.getStringParameter(request, "id", null);
        if (StringUtils.isBlank(id)) {
            return ResponseUtils.warn("参数错误");
        }
        DailyLifeActivityAssessment record = dailyLifeActivityAssessmentService.load(id);
        if (!ObjectUtils.isEmpty(record)) {
            Elder elder = elderService.load(record.getElderId());
            record.setElderName(elder.getName());
        }
        return ResponseUtils.success(record);
    }

    /**
     * 分页查询日常生活活动评估表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/dailyLifeActivityAssessment/list", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response list(HttpServletRequest request, HttpServletResponse response) {
        String name = ServletRequestUtils.getStringParameter(request, "name", null);
        Map<String, Object> params = new HashMap<String, Object>();
        if (!StringUtils.isBlank(name)) {
            params.put("elderName", name);
        }
        initParams(params, request);
        Page<DailyLifeActivityAssessment> page = dailyLifeActivityAssessmentService.queryPage(params);
        return ResponseUtils.success(page);
    }

    /**
     * 批量删除日常生活活动评估表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/dailyLifeActivityAssessment/delete", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response delete(HttpServletRequest request, HttpServletResponse response) {
        String ids = ServletRequestUtils.getStringParameter(request, "ids", null);
        if (StringUtils.isBlank(ids)) {
            return ResponseUtils.warn("参数错误");
        }
        dailyLifeActivityAssessmentService.remove(ids.split(","));
        return ResponseUtils.success("删除成功");
    }
}
